<template>
  <div class="metadata_management_FWQGL">
    <div class="pubBig Privilege">
      <div class="h0">
        <p class="lt"></p>
      </div>
      <div class="dDownMain">
        <div
          class="twoBox"
          :style="this.$route.query.Token ? 'top:50px;left:10px':'top: 100px;padding:10px'"
        >
          <el-row>
            <el-col :span="24">
              <el-button
                style="float:right"
                size="small"
                icon="el-icon-plus"
                @click="open"
              >新增服务器</el-button>
              <el-input
                v-model="search_val"
                size="small"
                style="display:inline-block;width:20%;float:left;margin-right:10px"
                placeholder="请输入服务器名称/服务器认证编码"
              ></el-input>
              <el-button
                icon="el-icon-search"
                size="small"
                style="float:left"
                @click="page.page,getTableData()"
              ></el-button>
              <el-select
                style="margin-left:15px"
                v-model="serverType"
                placeholder="请选择"
                @change="getTableData()"
                size="small"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
          <template>
            <el-table
              :data="tableData"
              style="width: 100%;margin-top:10px"
              v-loading="isLoading"
              height="600"
            >
              <el-table-column
                label="序号"
                type="index"
              >
              </el-table-column>
              <el-table-column
                prop="ServerType"
                label="服务器类型"
              >
              </el-table-column>
              <el-table-column
                prop="ServerName"
                label="服务器名称"
              >
              </el-table-column>
              <el-table-column
                prop="ServerIp"
                label="服务器ip地址"
              >
              </el-table-column>
              <el-table-column
                prop="ServerPort"
                label="服务端口"
              >
              </el-table-column>
              <el-table-column
                prop="ServerOS"
                label="操作系统"
              >
              </el-table-column>
              <el-table-column
                prop="SnmpPort"
                label="snmp端口"
              >
              </el-table-column>
              <el-table-column
                prop="SnmpName"
                label="社区名称"
              >
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <!-- <el-button
                    type="text"
                    size="mini"
                  >详情</el-button> -->
                  <el-button
                    size="mini"
                    icon="el-icon-edit"
                    @click="item_edit(scope.row)"
                  ></el-button>
                  <el-button
                    size="mini"
                    icon="el-icon-delete"
                    @click="item_del(scope.row.Id)"
                  ></el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
          <div class="block">
            <el-pagination
              style="text-align:center"
              layout="prev, pager, next"
              :total="page.total"
              :page-size="page.limit"
              @current-change="page_change"
              :current-page="page.page"
            >
            </el-pagination>
          </div>
        </div>
        <el-dialog
          :title="title"
          :visible.sync="dialogVisible"
          width="45%"
          :before-close="handleClose"
          :append-to-body="true"
          :close-on-click-modal="false"
        >
          <el-form
            label-position="right"
            label-width="120px"
            :model="formLabelAlign"
          >
            <el-form-item
              label="服务器类型"
              style="display:inline-block;width:50%"
            >
              <!-- <el-input v-model="formLabelAlign.ServerType"></el-input> -->
              <el-select
                style="width:100%"
                v-model="formLabelAlign.ServerType"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="服务器名称"
              style="display:inline-block;width:50%"
            >
              <el-input v-model="formLabelAlign.ServerName"></el-input>
            </el-form-item>
            <el-form-item
              label="服务器ip地址"
              style="display:inline-block;width:50%"
            >
              <el-input v-model="formLabelAlign.ServerIp"></el-input>
            </el-form-item>
            <el-form-item
              label="服务端口"
              style="display:inline-block;width:50%"
            >
              <el-input v-model="formLabelAlign.ServerPort"></el-input>
            </el-form-item>
            <el-form-item
              label="操作系统"
              style="display:inline-block;width:50%"
            >
              <el-input v-model="formLabelAlign.ServerOS"></el-input>
            </el-form-item>
            <el-form-item
              label="snmp端口"
              style="display:inline-block;width:50%"
            >
              <el-input v-model="formLabelAlign.SnmpPort"></el-input>
            </el-form-item>
            <el-form-item
              label="社区名称"
              style="display:inline-block;width:50%"
            >
              <el-input v-model="formLabelAlign.SnmpName"></el-input>
            </el-form-item>
          </el-form>
          <span
            slot="footer"
            class="dialog-footer"
          >
            <el-button @click="handleClose">取 消</el-button>
            <el-button
              type="primary"
              @click="btn_Ok"
            >保 存</el-button>
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  data () {
    return {
      tableData: [],
      isLoading: false,
      search_val: '',
      page: {
        page: 1,
        limit: 10,
        total: 1
      },
      dialogVisible: false,
      formLabelAlign: {},
      title: '',
      serverType: 'Product',
      options: [{
        value: 'Product',
        label: '生产端'
      }, {
        value: 'ODS',
        label: 'ODS'
      }, {
        value: 'DbCenter',
        label: '数据中心'
      }, {
        value: 'Other',
        label: '其他'
      }],
    };
  },
  methods: {
    getTableData () {//获取表格数据
      this.isLoading = true
      this.$post("/MeatDataManage/Server/List?keyword=" + this.search_val + "&page=" + this.page.page + "&limit=" + this.page.limit + "&serverType=" + this.serverType).then(res => {
        if (res.code == 0) {
          this.tableData = res.data.list
          this.page.total = res.data.TotalCount
          this.isLoading = false
        }
      })
    },
    page_change (val) {//切换页码
      this.page.page = val
      this.getTableData()
    },
    item_edit (val) {//编辑
      var info = JSON.stringify(val)
      this.formLabelAlign = JSON.parse(info)
      // this.dialogVisible = true
      this.open()
    },
    item_del (id) {//删除
      this.$confirm("确定删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(_ => {
          this.$post("/MeatDataManage/Server/Delete/" + id).then(res => {
            if (res.code == 0) {
              this.getTableData()
              this.$message({
                message: '操作成功',
                type: 'success'
              });
            } else {
              this.$message({
                message: res.msg,
                type: 'error'
              });
            }
          }).catch(err => {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          })
        })
        .catch(_ => { });
    },
    open () {//开启弹层
      this.formLabelAlign.Id ? this.title = '修改' : this.title = '新增'
      this.dialogVisible = true
    },
    handleClose () {//关闭弹层
      this.dialogVisible = false
      this.formLabelAlign = {}
    },
    btn_Ok () {//弹层确定事件
      let url = this.formLabelAlign.Id ? '/MeatDataManage/Server/Update' : '/MeatDataManage/Server/Create'
      let body = this.formLabelAlign
      this.$post(url, body).then(res => {
        if (res.code == 0) {
          this.getTableData()
          this.handleClose()
          this.$message({
            message: '操作成功',
            type: 'success'
          });
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      }).catch(err => {
        this.$message({
          message: res.msg,
          type: 'error'
        });
      })
    }
  },
  mounted () {
    // document.getElementById("2").focus();
  },
  created () {
    if (this.$route.query.Token) {
      localStorage.setItem('Token', this.$route.query.Token)
    }
    this.getTableData()
  },
  filters: {
    dateFormat (val) {
      if (val == null || val == '') {
        return ''
      } else {
        let d = new Date(val) // val 为表格内取到的后台时间
        let month = d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1
        let day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
        let hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours()
        let min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()
        let sec = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds()
        let times = d.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec
        return times
      }
    }
  }
};
</script>
<style lang="scss">
.metadata_management_FWQGL {
  .top-input {
    display: inline-block;
    width: 350px;
  }

  .box-top {
    margin-top: 15px;
    margin-left: 5px;
  }
}
</style>
